///////////////////////////////////
//Written during BSC 315 morning of 20100818
//This line is an edit for subversion testing.
//An edit to test the new working copy.

#include<DateTime.h>

//PIN DEFINITIONIS
//Should these be constants instead?
int dialpin			= 1; //Pin numbers are arbitrarily chosen, for now.
int irpin			= 2;
int redledpin		= 3;
int greenledpin		= 4;
int relaypin		= 5;

//VARIABLE DEFINITIONS
//Or should it be "variable constructors"?
int dialsetting		= 0;	//Will immediately be overwritten by checktimedial()
int endtimes		= 100;	//also will soon be overwritten, but 0 might cause wierd behavior. I should try it.
int triggerlevel	= 512;	//Needs to be tailored to the sensor chosen. Could be overwritten by a checktriggerdial.
							//And, while we're talking about possible features, I should mention that I'm
							//having some amorphous thoughts about a sort of vacation button.
String lightcolor	= red;	//Maybe do a simple int instead? NO! Human-readability is a noble goal!

void setup()		//What's up with the order of sections in these example sketches (see arduino.cc)?
					//They always seem to include:
					//	1. lots of ints at the beginning
					//	2. Usually, some functions defined.
					//	3. a void setup()
					//	4. a void loop(), where the program actually runs
					//The thing is, I think I've seen some examples (not on the arduino.cc site, and not
					//necessesarily in wiring, but definitely in some variant of C) that had some functions
					//used in a loop() or main() that weren't defined until after that loop(). I thought
					//this was impossible. Is this a http://en.wikipedia.org/wiki/False_memory_syndrome of mine?
{
	pinMode(dialpin, INPUT);
	pinMode(irpin, INPUT);
	pinMode(redledpin, OUTPUT);
	pinMode(greenledpin, OUTPUT);
	pinMode(relaypin, OUTPUT);
}

void checktimedial(void)
{
	if(digitalRead(dialpin) !== dialsetting)
	{
		dialsetting = digitalRead(dialpin);
		endtimes = dateTime.now() + 100*dialsetting;		//k=100 chosen arbitrarily. Needs to be selected
															//to give the dial a suitable scale, so that times
															//of a few minutes up to a couple days are equally selectable.
															//
															//Another thing--I was trying to do something earlier like
															//analogWrite(ledpin, digitalRead(dialpin)/1024*255), but it
															//would only work if I put digitalRead(dialpin)/4. Admittedly, I should
															//have realized that I meant 256, not 255, and that 255/1024 = 1/4,
															//but, the question is, was there something inherently wrong with the
															//expressed function call to analogWrite? Some rule about number
															//of operators or something?
	}
}

bool checktime(void)				//I'm using this correctly, right? the bool bit at the beginning is supposed to sepcify
									//the return type, and the void part inside is supposed to specify the argument(s) type.
									//for that matter, did I do it right with setlighst() below? Do I have to do 
									//void setlights(string lightcolor), or is defining the datatype there unnecessary, since
									//I defined it at the top of the sketch?
{
	if(DateTime.now() >= endtimes)
	{
		return(TRUE);
	}
	else
	{
		return(FALSE);
	}
}

void checkir(void)
{
	//Might need a dial to set triggerlevel like the one used to set dialsetting.
	if(digitalRead(irpin) >= triggerlevel)				//Or however I need to do this for the sensor I eventually acquire.
	{
		endtimes = DateTime.now() + 100*dialsettings;	//Same arbitrarily chosen k.
	}
}

void setlights(void)
{
	if(lightcolor == green)
	{
		digitalWrite(greenledpin, HIGH);
		digitalWrite(redledpin, LOW);
		digitalWrite(relaypin, HIGH);	//HIGH means the realy will be in it's non-normal state.
										//I'm making this the green light, so that, if something happens to the board, the
										//(separately powered) 110v lights will switch to red.
	}
	else
	{
		digitalWrite(greenledpin, LOW);
		digitalWrite(redledpin, HIGH);
		digitalWrite(relaypin, LOW);	//Now that I think about it, it would be a good idea to check what the actual voltate is
										//that's associated with LOW. I'm hoping it's ~=0V. HIGH, I'm pretty sure, is 5V, but
										//I should, of course, check that too.
	}
}

void loop()
{
	checktimedial();
	checktime();
	checkir();
	setlights();
}